<!DOCTYPE html>
<html>
<head>
    <title>原生JavaScript编写的模态框</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0, viewport-fit=cover">
    <style>

        /*模态框主体样式*/
        .modal {
	
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0, 0, 0, 0.5);
        }

        /*模态框公共样式*/
        .modal-content {
				border-radius: 10px;	
            background-color: #fefefe;
            margin: 50px auto 0;
            padding: 20px;
            border: 1px solid #888;
            box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
            animation: modal-show 0.3s;
        }

        /* 正常大小 */
        .modal-normal {
            width: 600px;
        }

        /* 中等大小 */
        .modal-medium {
            width: 400px;
        }

        /* 迷你大小 */
        .modal-mini {
            width: 250px;
        }

        /* 隐藏 */
        .modal-hide {
            animation: modal-hide 0.3s;
        }

        /* 展示动画 */
        @keyframes modal-show {
            from {
                opacity: 0;
                transform: translateY(-50px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        /* 隐藏动画 */
        @keyframes modal-hide {
            from {
                opacity: 1;
                transform: translateY(0);
            }
            to {
                opacity: 0;
                transform: translateY(-50px);
            }
        }

        /*关闭按钮*/
        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
            -webkit-tap-highlight-color: rgba(255,0,0,0);
        }

        /*关闭按钮的鼠标点击和经过样式*/
        .close:hover,
        .close:focus {
            color: black;
            text-decoration: none;
            cursor: pointer;
            -webkit-tap-highlight-color: rgba(255,0,0,0);
        }
    </style>
</head>
<body>
    <h2>模态框</h2>
    <button data-modal-action="open" data-modal-target="myModal">打开模态框</button>
<!-- 模态框区域 -->
    <div id="myModal" class="modal">
        <div class="modal-content modal-normal">
            <span class="close">&times;</span>
            <h3>模态框标题</h3>
            <p>模态框文字、段落等内容。</p>
        </div>
    </div>
<!-- 模态框 -->
    <script>

        // 获取触发模态框的自定义属性
        var modalButtons = document.querySelectorAll("[data-modal-action]");

        // 获取模态框主体
        var modals = document.querySelectorAll(".modal");

        // 监听模态框的按钮事件
        modalButtons.forEach(function(button) {
            button.addEventListener("click", function() {
                var targetId = this.dataset.modalTarget;
                var targetModal = document.getElementById(targetId);

                if (targetModal) {
                    var action = this.dataset.modalAction;

                    if (action === "open") {
                        openModal(targetModal);
                    } else if (action === "close") {
                        closeModal(targetModal);
                    }
                }
            });
        });

        // 打开模态框
        function openModal(modal) {
            modal.style.display = "block";
            modal.querySelector(".modal-content").classList.remove("modal-hide");
        }

        // 关闭模态框
        function closeModal(modal) {
            modal.querySelector(".modal-content").classList.add("modal-hide");
            modal.querySelector(".modal-content").addEventListener("animationend", function () {
                modal.style.display = "none";
            }, { once: true });
        }

        // 监听模态框的关闭时事件
        modals.forEach(function(modal) {
            var closeButton = modal.querySelector(".close");
            if (closeButton) {
                closeButton.addEventListener("click", function() {
                    var targetModal = this.closest(".modal");
                    closeModal(targetModal);
                });
            }
        });

        // 当用户点击模态框外部时，关闭模态框
        window.onclick = function (event) {
            modals.forEach(function(modal) {
                if (event.target === modal) {
                    closeModal(modal);
                }
            });
        };
    </script>
</body>
</html>